<?php
if(!defined('ADMINPX') || !defined('ADMIN')) {
	exit('Access Denied');
}
$act=$_GET['act'];
$id=$_GET['id'];
$pluginid=$_GET['pluginid'];

############################
if($act=='anz'){
   echo" <div class='admin'>安装插件<br/></div> ";
	    if(file_exists("source/plugin/".$id."/") && file_exists("source/plugin/".$id."/plugin.xml")){
				  $foop = fopen('source/plugin/'.$id.'/plugin.xml', 'r'); 
				  $xml  = fread( $foop, 5096 );
					fclose( $foop );
					preg_match( "/\<plugin_id\>(.*?)\<\/plugin_id\>/s", $xml, $pluginid ); 
					preg_match( "/\<plugin_name\>(.*?)\<\/plugin_name\>/", $xml, $pluginname ); 
					preg_match( "/\<plugin_title\>(.*?)\<\/plugin_title\>/", $xml, $plugintitle );
					preg_match( "/\<plugin_version\>(.*?)\<\/plugin_version\>/", $xml, $pluginbb );

					preg_match( "/\<plugin_ion\>(.*?)\<\/plugin_ion\>/", $xml, $pluginion );
					preg_match( "/\<plugin_css\>(.*?)\<\/plugin_css\>/", $xml, $plugincss );
					preg_match( "/\<plugin_js\>(.*?)\<\/plugin_js\>/", $xml, $pluginjs );
					preg_match( "/\<plugin_class\>(.*?)\<\/plugin_class\>/", $xml, $pluginclass );

					preg_match( "/\<plugin_agreement\>(.*?)\<\/plugin_agreement\>/", $xml, $plugincaxxy );
					preg_match( "/\<plugin_agreement_name\>(.*?)\<\/plugin_agreement_name\>/", $xml, $plugincaxxyname );
					preg_match( "/\<plugin_authorize\>(.*?)\<\/plugin_authorize\>/", $xml, $pluginAuthorize );


					if(!$pluginid[1] && !$pluginname[1] && !$pluginbb[1] ){
						 echo"插件安装页面出错！";
					}else if( $num_count=$db->get_one('plugin',"`identifier` = '$pluginid[1]' ",'identifier')){
						 echo"插件id重复！";			 		 
					}else if( $plugincaxxy[1] == '1' && !$_GET['xy'] ){
						 echo"<div class=\"azxy\">安装协议</div>
						 <div class=\"azxyname\">$plugincaxxyname[1]</div>
						 <div class=\"azxy\">
						 <a href=\"admin.php?action=plugin&act=anz&id=$pluginid[1]&xy=1&bvb=1\">同意并遵守</a> /
						 <a href=\"admin.php?action=plugin&\">不同意返回</a> </div>
						 ";
					}else{
						 	if($db->insert('plugin',array('pluginid' => 'NULL','available' => 0,'adminid' => 0,'name' => $pluginname[1],'identifier' => $pluginid[1],'descript' => $plugintitle[1],'description' => $pluginion[1],'descriptcss' => $plugincss[1],'descriptjs' => $pluginjs[1],'descriptclass' => $pluginclass[1],'datatables' => '','directory' => $pluginid[1].'/','copyright' => $pluginzz[1],'modules' => 0,'version' => $pluginbb[1]))){
					 		      $pid=$db->insert_id();
							 		  if(file_exists("source/plugin/$id/install.sql")){
						 		  	   hfsjk_sql("source/plugin/".$id."/install.sql");
						 		    }
						 		   $xml_array=simplexml_load_file('source/plugin/'.$id.'/plugin.xml');
                   foreach($xml_array as $tmp ){
                      if($pid && $tmp->uname && $tmp->utitle){
                            $db->insert('pluginubb',array('id' => 'NULL','pluginid' => $pid,'identifier' => $pluginid[1],'name' => $tmp->uname,'content' => $tmp->utitle));
                      }
                      if($pid && $tmp->name && $tmp->variable && $tmp->type && !$db->get_one('pluginvar',"`variable` = '$tmp->variable' ",'pluginvarid')){
                            $db->insert('pluginvar',array('pluginvarid' => 'NULL','pluginid' => $pid,'identifier' => $pluginid[1],'displayorder' => $pluginid[1],'title' => $tmp->name,'description' => $tmp->title,'variable' => $tmp->variable,'type' => $tmp->type,'value' => $tmp->value,'extra' => $tmp->extra));
                      }
                   }
						 		 $fp=fopen("source/plugin/".$id."/anzcg.gke","w");
                 $write=fputs($fp,"安装成功！$xml_array $tmp->uname");
                 fclose($fp);
						     echo"安装成功！";
						     setcookie($id,'');
						 	}else{
						     echo"安装出错！";
						  }
				  }
			}else{
				echo"插件安装页面出错！";
			}


   echo "<br/><a href=\"admin.php?action=plugin\">返回插件管理</a><br/>";
}else if($act=='Uninstall'){
   echo" <div class='admin'>卸载应用</div>";
   echo "注意：卸载后存储的数据将被清除<br/><a href=\"admin.php?action=plugin&act=Uninstallok&id=$id\">确定卸载应用</a>";
   echo "<br/><a href=\"admin.php?action=plugin\">返回应用管理</a><br/>";
}else if($act=='Uninstallok'){
   echo" <div class='admin'>卸载应用<br/></div>";
	    $pppp=file_exists("source/plugin/".$id."/");
	    $waa=$db->get_one('plugin',"`identifier` ='$id' ",'');
	    $install=file_exists("source/plugin/".$id."/install.sql");
			if($pppp ){
				  if(!$id){
						 echo"应用id无效！";
					}else if(!$waa){
						 echo"没有此应用！";
					}else{
						 if(file_exists('source/plugin/'.$id.'/Uninstall.php')){
						 	require_once 'source/plugin/'.$id.'/Uninstall.php';
						  foreach($Uninstall as $lists){
                      $lists=str_replace("bz_",$_config['mysql']['qianh'],$lists);
                      $sqll="DROP TABLE $lists ";
 						          mysql_query($sqll);
						  }
						 }
						 $db->delete('plugin',"`pluginid` = '$waa[pluginid]'");
             $db->delete('pluginvar',"`pluginid` = '$waa[pluginid]'");
             $db->delete('pluginubb',"`pluginid` = '$waa[pluginid]'");
						 @unlink("source/plugin/".$id."/anzcg.gke");
						 echo"卸载成功！";
			    }
			}else{
				echo"应用id无效！";
			}
   echo "<br/><a href=\"admin.php?action=plugin\">返回应用管理</a><br/>";
}else if($act=='kaiq'){
   $db->update('plugin',array('available' => '1')," `identifier`= '$id'");
   echo" <div class='admin'>管理应用<br/></div> 开启应用成功 <br/>";
   echo "正在自动返回.......<br/><a href=\"admin.php?action=plugin\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=plugin'>";
}else if($act=='guanb'){
   $db->update('plugin',array('available' => '0')," `identifier`= '$id'");
   echo" <div class='admin'>管理应用<br/></div> 关闭应用成功 <br/>";
   echo "正在自动返回.......<br/><a href=\"admin.php?action=plugin\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=plugin'>";
}else if($act=='sezi'){
   echo" <div class='admin'>管理应用<br/></div> ";
   if($_GET[aa] =='ok' && $_POST){
   	    $db->update('plugin',array('adminid' => $_POST[syxs])," `pluginid`= '$id'");
	      $tzfl=$db->query('pluginvar',"`pluginid` ='$id' ",'');
        while($flm=mysql_fetch_array($tzfl)){
     	        $njglm=$flm[pluginvarid];
              $db->update('pluginvar',array('value' => $_POST[$njglm])," `pluginvarid`= '$flm[pluginvarid]'");
	      }
        echo"设置应用成功<br/> ";
        echo "正在自动返回.......<br/><a href=\"admin.php?action=plugin&act=sezi&id=$id\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=plugin&act=sezi&id=$id'>";
   }else{
     echo "<form method=\"POST\" action=\"admin.php?action=plugin&act=sezi&aa=ok&id=$id&cid=$id\">";
     
     $rowaxds=$db->get_one('plugin'," `pluginid` = '$id'",'');
     if($rowaxds[adminid] == '0'){ $syxsa="CHECKED "; }else{ $syxsb="CHECKED "; }
     if($rowaxds[descriptuser] == '1'){ $syxsua="CHECKED "; }else{ $syxsub="CHECKED "; }
     echo '
     <div class="admin_a">应用显示
     <input type="radio" '.$syxsa.' name="syxs" value="0" />显示
     <input type="radio" '.$syxsb.' name="syxs" value="1" />隐藏</div>
     ';

	   $tzfl=$db->query('pluginvar',"`pluginid` ='$id'  ORDER BY `pluginvarid`",'');
     while($flm=mysql_fetch_array($tzfl)){
     	  if($flm[type] == 'textarea'){
     	  	 $name='<textarea class="textarea" type="text" name="'.$flm[pluginvarid].'">'.$flm[value].'</textarea>';
     	  }elseif($flm[type] == 'radio'){
     	  	 $name='';
     	  	 $ArrFiltrate=explode(',',$flm[extra]);
           foreach ($ArrFiltrate as $key){
           	    $ssid=explode("=",$key);
           	    if(!$ssid[1]){$ssid[1]=$ssid[0];}
           	    $syxsua='';
           	    if($ssid[0] == $flm[value]){$syxsua="CHECKED ";}
           	    $name=$name. '<input type="radio" '.$syxsua.' name="'.$flm[pluginvarid].'" value="'.$ssid[0].'" />'.$ssid[1].'';
           }
     	  }else{
     	  	 $name='<input class="text" type="'.$flm[type].'" name="'.$flm[pluginvarid].'" value="'.$flm[value].'"/>';
        }     		
     	  echo "<div class='admin_a'>$flm[title]: $name <br/>(<font color='#ff0000'>$flm[description]</font>)<br/></div>";
     }
     echo '<input class="submit" type="submit" name="adminpost" value="提交"/></form><br/>';
   }
   echo "<a href=\"admin.php?action=plugin\">返回应用管理</a><br/>";  
}else if($act=='gx'){

}else if($act=='xiazai'){
function deldir($dir){
   $dh = opendir($dir);
   $a='0';
   while ($file = readdir($dh)){
      if ($file != "." && $file != ".."){
         $fullpath = $dir . "/" . $file;
         if (!is_dir($fullpath)){
            unlink($fullpath);
            @rmdir($fullpath);
            $a++;
         } else{
            $b=deldir($fullpath);
            @rmdir($fullpath);
            $a=$a+$b;
         }
      }
   }
   closedir($dh);
   return $a;
}
   echo" <div class='admin'>在线下载应用<br/></div>";
   if(!$_GET['url'] or !$_GET['addon_id']){
       echo '数据出错！<br/>';
       echo "正在自动返回.......<br/><a href=\"admin.php?action=plugin\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=plugin'>";
   }elseif($db->get_one('plugin',"`pluginid` = '$_GET[addon_id]' ",'')){
       echo '应用已经安装！请卸载后下载！<br/>';
       echo "正在自动返回.......<br/><a href=\"admin.php?action=plugin\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=plugin'>";
   }else{
      $bzwjc=Bingz_upload_Remote('cache/',$_GET['url'],'zip');
    	if($bzwjc[1] == '1'){
    		      if(file_exists('source/plugin/'.$_GET['addon_id'].'')){
                 deldir('source/plugin/'.$_GET['addon_id'].'');
              }
			        $sjwe=zip("cache/".$bzwjc[2],'source/plugin');
			        if($sjwe){
			            echo"在线下载成功！<br/>";
			        }else{
                  echo"在线下载失败！<br/>";
		          }
      }else{
		          echo"下载文件失败！<br/>";
      }
       echo "正在自动返回.......<br/><a href=\"admin.php?action=plugin\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=plugin'>";
   }
}else if($_GET['actadminfl'] == 'admin'){
      $pod=$_GET['pod'];
      if(!$pod){$pod='index';}
      echo" <div class='admin'>应用后台<br/></div>";
      $row=$db->get_one('plugin',"`pluginid` = '$pluginid' ",'');
      define('BZ_'.$row[identifier].'', TRUE);
      $plugin_dz='index.php?'.$Bingz_ml.'=plugin&lnsert='.$row[identifier].'&';
      $plugin_admindz='admin.php?action=plugin&actadminfl=admin&pluginid='.$row[pluginid].'&';
   
      @require_once 'source/plugin/'.$row[directory].'admin/'.$pod.'.php';

   echo "<a href=\"$plugin_admindz\">返回应用后台</a><br/>";  
   echo "<a href=\"admin.php?action=plugin\">返回应用管理</a><br/>";  
}else if($_GET['actadminfl'] == 'plugin'){
$Bingz_k7t_username=Bingz_config('Bingz_k7t_username');
$Bingz_k7t_userpass=Bingz_config('Bingz_k7t_userpass');
echo"<a href=\"admin.php?action=plugin\">应用管理</a> <div class='admin'>应用中心</div> ";
echo'
<center>
<iframe src="'.$Bingz_url_addon.'&url='.$url.'&ip='.$serverip.'&name='.$wzxx_title.'&username='.$Bingz_k7t_username.'&userpass='.$Bingz_k7t_userpass.'" name="test" align="MIDDLE" width="100%" height="500" marginwidth="" marginheight="" frameborder=""> 
很抱歉，馈下使用的浏览器并不支援 IFrame，不能正常浏览应用中心。</iframe> 
</center>
';
}else{
     echo "<a href=\"admin.php?action=plugin&actadminfl=plugin\">应用中心</a><br/>";  
     echo"<div class='admin'>已启用应用<br/></div>";
     $sql=$db->query('plugin',"`available` = '1' ORDER BY `pluginid` ",'');
     while($row=mysql_fetch_array($sql)){
     $mm='';
     $type_txtht='';
       if($row[description]){$mm='<img src="source/plugin/'.$row[directory].$row[description].'" width="40" height="40" />';}
       if( file_exists('source/plugin/'.$row[directory].'admin/index.php')){
			   $type_txtht="<a href=\"admin.php?action=plugin&actadminfl=admin&pluginid=$row[pluginid]\">后台</a>  ";
       }
       $plugin_dz='index.php?'.$Bingz_ml.'=plugin&insert='.$row[identifier].'';

			 echo"<div class='admin_a'>$mm<b>$row[name](<font color='#ff0000'>$row[version]</font>)</b> 
			 <br/>地址：<a href=\" $plugin_dz\" target=\"_blank\" >访问插件</a> 
			 <br/>$type_txtht
			 <a href=\"admin.php?action=plugin&act=sezi&id=$row[pluginid]\">设置</a> 
			 <a href=\"admin.php?action=plugin&act=guanb&id=$row[identifier]\">关闭</a>  
			 <a href=\"admin.php?action=plugin&act=gx&id=$row[identifier]\">更新</a>  
			 <a href=\"admin.php?action=plugin&act=Uninstall&id=$row[identifier]\">卸载</a> <br/> </div>";
     }
      
     echo"<div class='admin'>未启用插件<br/></div>";
     $sql=$db->query('plugin',"`available` = '0' ORDER BY `pluginid` ",'');
     $mm='';
     while($row=mysql_fetch_array($sql)){
       if($row[description]){$mm='<img src="source/plugin/'.$row[directory].$row[description].'" width="40" height="40" />';}
			 echo"<div class='admin_a'>$mm<b>$row[name](<font color='#ff0000'>$row[version]</font>)</b>
			 <br/>
			 <a href=\"admin.php?action=plugin&act=kaiq&id=$row[identifier]\">启用</a>
			 <a href=\"admin.php?action=plugin&act=Uninstall&id=$row[identifier]\">卸载</a></div> ";
     }
  echo"<div class='admin'>未安装应用<br/></div> <div class='forum_a'></div> ";
  $dir = opendir("source/plugin/");
  while($file = readdir($dir)){
		if($file !='.' && $file !='..' && !strpos($file,'.')){
			if(!file_exists("source/plugin/".$file."/anzcg.gke") && file_exists("source/plugin/".$file."/plugin.xml")){
				$foop = @fopen('source/plugin/'.$file.'/plugin.xml', 'r'); 
				$xml  = @fread( $foop, 5096 );
					@fclose( $foop );  
          $mm='';
					preg_match( "/\<plugin_id\>(.*?)\<\/plugin_id\>/s", $xml, $pluginid ); 
					preg_match( "/\<plugin_name\>(.*?)\<\/plugin_name\>/", $xml, $pluginname ); 
					preg_match( "/\<plugin_title\>(.*?)\<\/plugin_title\>/", $xml, $plugintitle );
					preg_match( "/\<plugin_version\>(.*?)\<\/plugin_version\>/", $xml, $pluginbb );
					preg_match( "/\<plugin_ion\>(.*?)\<\/plugin_ion\>/", $xml, $pluginion );
					preg_match( "/\<plugin_author\>(.*?)\<\/plugin_author\>/", $xml, $pluginzz );
       	  if($pluginion[1]){$mm='<img src="source/plugin/'.$pluginid[1].'/'.$pluginion[1].'" width="40" height="40" />';}
			    echo"<div class='admin_a'>$mm<b>$pluginname[1](<font color='#ff0000'>$pluginbb[1]</font>)</b> 
			    <a href=\"admin.php?action=plugin&act=anz&id=$pluginid[1]\"><font color='#ff0000'> 安装 </font></a>
			    <br/>作者：<font color='#ff0000'>$pluginzz[1]</font>
			    <br/>简介：<font color=\"#999999\">$plugintitle[1]</font></div>";
			}
		}
	}
  closedir($dir);
}
?>